package com.canoe.text.processor;

import com.canoe.common.CanoeException;
import com.canoe.common.Log;
import com.canoe.parameter.ParameterParser;
import com.canoe.text.AbstractTextOrderProcessor;
import com.canoe.text.common.TextOrder;
import com.canoe.text.common.TextParameter;
import com.canoe.text.common.TextParameterValue;
import com.canoe.text.handler.TextRegexExtractHandler;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * text文件提取操作适配器
 *
 * @author: btm
 * @time: 21:56 2020/9/21
 **/
public final class TextExtractProcessor extends AbstractTextOrderProcessor {

    @Override
    protected TextOrder support() {
        return TextOrder.EXTRACT;
    }

    @Override
    protected void doProcess(String... args) {
        ParameterParser<TextParameterValue> parameterParser = new ParameterParser<>();
        TextParameterValue pv = parameterParser.parser(TextParameterValue.class, TextParameter.REGEX, args);
        if (StringUtils.isNotBlank(pv.getRegex()) && CollectionUtils.isNotEmpty(pv.getFileNameList())) {
            TextRegexExtractHandler.regexExtractHandler(pv);
        } else {
            throw new CanoeException("this text order [extract] lack parameter, please check again");
        }
        Log.println("目标写入文件的路径及目标文件名："+pv.getTargetFilePath());
        Log.println("text extract success finish.");
    }

}
